package lc;

import org.junit.Test;

public class Ex151 {
    class Solution {
        public String reverseWords(String s) {
            String res = "";
            int idx = 0, cur = 0, start = -1;
            // char last = ' ';
            while (s.length() > 0 && idx < s.length()){
                char c = s.charAt(idx);
                if (c == ' ') {
                    if (start >= 0) {
                        String tmp = s.substring(start, start + cur);
                        res = tmp + " " + res;
                        s = s.substring(idx);
                        start = -1;
                        idx = -1;
                        cur = 0;
                    }
                } else {
                    if (start == -1) {
                        start = idx;
                    }
                    cur++;
                }
                idx++;
            }

            if (start >= 0) {
                String tmp = s.substring(start, start + cur);
                res = tmp + " " + res;
            }
            return res.substring(0, res.length() - 1);
        }
    }

    @Test
    public void test() {
        Solution s = new Solution();
        System.out.println(s.reverseWords("  Bob    Loves  Alice   "));
    }
    
}
